﻿''' <summary>
''' Use this Filestream Class for faster loading and writing!
''' </summary>
''' <remarks></remarks>
Public Class Filestream
    Inherits IO.FileStream
    Const Bytelänge As Integer = 1024
    Class kBytes
        Dim Bytes As Byte()
        Dim Position As Integer
        Dim MaxPosition As Integer
        Public Sub New(ByVal Bytes As Byte(), ByVal MaxPosition As Integer)
            Me.Bytes = Bytes
            Me.MaxPosition = MaxPosition
            Position = 0
        End Sub
        Public Sub New()
            ReDim Bytes(Bytelänge - 1)
            MaxPosition = Bytelänge
        End Sub
        Public Function GetData() As Byte()
            Return Bytes
        End Function
        Public Function GetPosition() As Integer
            Return Position
        End Function
        Public Function WriteByte(ByVal B As Byte) As Boolean
            Bytes(Position) = B
            Position += 1
            If Position >= Bytelänge Or Position >= MaxPosition Then
                Return True
            Else
                Return False
            End If
        End Function
        Public Function ReadByte() As Byte()
            Position += 1
            If Position >= Bytelänge Or Position >= MaxPosition Then
                Return New Byte() {Bytes(Position - 1), 1}
            End If
            Return New Byte() {Bytes(Position - 1), 0}
        End Function
        Public Function Seek(ByVal offset As Long) As Long
            If offset < 0 Then
                If offset + Position < 0 Then
                    Dim R As Long = offset + Position
                    Position = 0
                    Return R
                Else
                    Position += offset
                    Return 0
                End If
            Else
                If offset + Position > MaxPosition Or offset + MaxPosition > Bytelänge Then
                    Dim R As Long = offset - (MaxPosition - Position)
                    Position = MaxPosition
                    Return R
                Else
                    Position += offset
                    Return 0
                End If
            End If
        End Function
    End Class
    Dim Data As New List(Of kBytes)
    Dim RWPosition As Long = 0

    Public Sub New(ByVal Pfad As String, ByVal Mode As IO.FileMode, ByVal Access As IO.FileAccess)
        MyBase.New(Pfad, Mode, Access)
        Select Case Mode
            Case IO.FileMode.Open
                Dim B(MyBase.Length - 1) As Byte
                MyBase.Read(B, 0, MyBase.Length)

                Dim MaxLänge As Long = MyBase.Length
                Dim Pos As Long = 0
                While MaxLänge > 0
                    Dim Bytes(Bytelänge - 1) As Byte
                    Dim Len As Long = Bytelänge
                    If Bytelänge > MaxLänge Then Len = MaxLänge
                    Array.Copy(B, Pos, Bytes, 0, Len)
                    MaxLänge -= Bytelänge
                    Pos += Bytelänge
                    Data.Add(New kBytes(Bytes, Len))
                End While
            Case IO.FileMode.OpenOrCreate
                Data.Add(New kBytes())
        End Select
    End Sub
    Public Overrides Function ReadByte() As Integer
        Dim B() As Byte = Data(RWPosition).ReadByte
        If B(1) > 0 Then
            If RWPosition < Data.Count - 1 Then
                RWPosition += 1
            End If
        End If
        Return B(0)
    End Function
    Public Overrides Sub WriteByte(ByVal B As Byte)
        If Data(Data.Count - 1).WriteByte(B) Then
            Data.Add(New kBytes)
        End If
    End Sub
    Public Shadows Sub Seek(ByVal offset As Long)
        If offset > 0 Then
            While offset > 0
                offset = Data(RWPosition).Seek(offset)
                If offset > 0 Then
                    RWPosition += 1
                End If
                If RWPosition > Data.Count - 1 Then
                    RWPosition = Data.Count - 1
                    Exit While
                End If
            End While
        Else
            While offset < 0
                offset = Data(RWPosition).Seek(offset)
                If offset < 0 Then
                    RWPosition -= 1
                End If
                If RWPosition < 0 Then
                    RWPosition = 0
                    Exit While
                End If
            End While
        End If
    End Sub
    Public Overrides Sub Close()
        If MyBase.CanWrite Then
            For I = 0 To Data.Count - 1
                MyBase.Write(Data(I).GetData, 0, Data(I).GetPosition)
            Next
        End If
        MyBase.Close()
    End Sub
End Class
